﻿/*---------------------------------------------------------------------------------------
 
	File Name:			common.js	
	Create Time:		Jun 17th, 2007.
	Author:				Seamuz
	All Rights Reserved (c) Huohai Co.,Ltd
 
----------------------------------------------------------------------------------------*/

var useragent	= navigator.userAgent.toLowerCase();
var webtv		= useragent.indexOf('webtv') != -1;
var kon			= useragent.indexOf('konqueror') != -1;
var mac			= useragent.indexOf('mac') != -1;
var saf			= useragent.indexOf('applewebkit') != -1 || navigator.vendor == 'Apple Computer, Inc.';
var opera		= useragent.indexOf('opera') != -1 && opera.version();
var moz			= (navigator.product == 'Gecko' && !saf) && useragent.substr(useragent.indexOf('firefox') + 8, 3);
var ns			= useragent.indexOf('compatible') == -1 && useragent.indexOf('mozilla') != -1 && !opera && !webtv && !saf;
var ie			= (useragent.indexOf('msie') != -1 && !opera && !saf && !webtv) && useragent.substr(useragent.indexOf('msie') + 5, 3);

function $(elementid) {
	return document.getElementById(elementid);
}

function undefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

function empty(str) {
	return typeof str == 'undefined' || str == null || str == '';
}

function trim(str) {
	return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
}

function htmlencode(str) {
	str = str.replace(/&/g, '&amp;');
	str = str.replace(/</g, '&lt;');
	str = str.replace(/>/g, '&gt;');
	str = str.replace(/(?:\t| |\v|\r)*\n/g, '<br />');
	str = str.replace(/  /g, '&nbsp; ');
	str = str.replace(/\t/g, '&nbsp; &nbsp; ');
	str = str.replace(/\x22/g, '&quot;');
	str = str.replace(/\x27/g, '&#39;');
	return str;
}

function htmldecode(str) {
	str = str.replace(/&amp;/gi, '&');
	str = str.replace(/&nbsp;/gi, ' ');
	str = str.replace(/&quot;/gi, '"');
	str = str.replace(/&#39;/g, "'");
	str = str.replace(/&lt;/gi, '<');
	str = str.replace(/&gt;/gi, '>');
	str = str.replace(/<br[^>]*>(?:(\r\n)|\r|\n)?/gi, '\n');
	return str;
}

function textencode(str) {
	str = str.replace(/&amp;/gi, '&');
	str = str.replace(/</g, '&lt;');
	str = str.replace(/>/g, '&gt;');
	return str;
}

function textdecode(str) {
	str = str.replace(/&amp;/gi, '&');
	str = str.replace(/&lt;/gi, '<');
	str = str.replace(/&gt;/gi, '>');
	return str;
}

function specialchar(str) {
	var chars = "!@#$%^&*()+=|\/?<>,.:;'\"`[]{}";
	for (var i=0; i<chars.length; i++)
	{
		if ( str.indexOf(chars.substring(i, i + 1)) != -1 )
		{
			return true;
		}
	}
	return false;
}

function bytes(str) {
	var len = 0;
	for (i = 0; i < str.length; len += (str.charCodeAt(i++) < 255 ? 1 : 2));
	return len;
}

function form(f) {
	switch (typeof f) {
		default:
		case null:
		case '':
		case 'undefined':
			return document.forms[0];
		case 'string':
		case 'number':
			return document.forms[f];
		case 'object':
			return f;
	}	
}

function forminlist(arr) {
	for(var i in arr) {
		var tmpform = form(arr[i]);
		if(!undefined(tmpform)) return tmpform;
	}
	return null;
}

function msg(message, focusctrl) {
	alert(message);
	if (focusctrl != null) {
		focusctrl.focus();
	}
	return false;
}

function offset(element) {
	var point = { x: element.offsetLeft, y: element.offsetTop };
	if (element.offsetParent) {
		var parentPoint = offset(element.offsetParent);
		point.x += parentPoint.x;
		point.y += parentPoint.y;
	}
	return point;
}

function inarray(obj, arr) {
	if(typeof obj == 'string') {
		for(var i in arr) {
			if(arr[i] == obj) {
					return true;
			}
		}
	}
	return false;
}

function checkallbox(box)
{
	var f = box.form;
	var ischecked = box.checked;
	for (var i=0; i<f.elements.length; i++)
	{
		if (f.elements[i].type.toLowerCase() == "checkbox")
		{
			f.elements[i].checked = ischecked;
		}
	}
}

function basepath() {
	var path = location.href.replace(/^https?:\/\/[^\/]+(\/.*)$/gi, "$1");
	path = path.substring(0, path.lastIndexOf("/"));
	path = path.replace(/\/(?:behind|plugin|archive)$/i, '');
	if (path == '/') {
		return '';
	}
	return path;
}

function setcookie(name,value, days)
{ 
	var exp = new Date();
	exp.setTime(exp.getTime() + days*24*60*60*1000);
	document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString();
}

function getcookie(name)
{
	var arr = document.cookie.match(new RegExp("[\s\S]*"+ name +"=([^;]*)(;|$)"));
	if(arr != null)
		return unescape(arr[1]); 
	return null;
}

function isimg(src)
{
	var ext = ['.gif', '.jpg', '.jpeg', '.png'];
	var s = src.toLowerCase();
	var r = false;
	for(var i = 0; i < ext.length; i++)
	{
		if (s.indexOf(ext[i]) > 0)
		{
			r = true;
			break;
		}
	}	
	return r;
}

function switchfolder(img, element)
{
	if ($(element).style.display == '')
	{
		img.src = img.src.replace("folder_open", 'folder_close');
		img.alt = '展开';
		$(element).style.display = 'none';
		var cookievalue = getcookie('closedfolder');
		cookievalue += element + '&';
		setcookie('closedfolder', cookievalue, 30);
	}
	else
	{
		img.src = img.src.replace("folder_close", 'folder_open');
		img.alt = '收缩';
		$(element).style.display = '';
		var cookievalue = getcookie('closedfolder');
		cookievalue = cookievalue.replace(element + '&', '');
		setcookie('closedfolder', cookievalue, 30);
	}
}

function initfolder()
{
	var cookievalue = getcookie('closedfolder');
	if (cookievalue != null && cookievalue != '')
	{
		folders = cookievalue.split('&');
		for(var i = 0; i < folders.length - 1; i++)
		{
			var folder = $(folders[i]);
			if (folder != null)
			{
				folder.style.display = 'none';
				$('btn_' + folders[i]).src = $('btn_' + folders[i]).src.replace('folder_open', 'folder_close');
				$('btn_' + folders[i]).alt='展开';
			}
		}
	}
}

function jump(obj)
{
	var selectedValue = obj.options[obj.selectedIndex].value;
	if (selectedValue.indexOf(',') != -1 || selectedValue == -1)
	{
		obj.options[0].selected = true;
		return;
	}
	location.href = 'board.aspx?boardid=' + selectedValue;
}

function checkbyvalue(c, v)
{	
	if (!undefined(c.options))
	{
			
		for (var i = 0; i < c.options.length; i++)
		{
			if (c.options[i].value == v)
			{
				c.options[i].selected = true;
				break;
			}
		}
	}
	
}

function checkradiobyvalue(c, v)
{
	if (!undefined(c.item))
	{
		for (var i = 0; i < c.item.length; i++)
		{
			if (c.item(i).value == v)
			{
				c.item(i).checked = true;
				break;
			}
		}
	}
	
}

function switchlogin(c)
{
	var loginform = $(c);
	if(loginform.style.display == '' )
		loginform.style.display = 'none';
	else
		loginform.style.display = '';
		
}

// --- 获取ClassName
document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var j = 0; j < elem.length; j++) {
		var classes = elem[j].className;
		if (myclass.test(classes)) retnode.push(elem[j]);
	}
	return retnode;
}

function getOffset(el) {
	var point = { x: el.offsetLeft, y: el.offsetTop };
	//Recursion
	if (el.offsetParent) {
		var parentPoint = getOffset(el.offsetParent);
		point.x += parentPoint.x;
		point.y += parentPoint.y;
	}
	return point;
}

function getScrollOffset() {
	var point;
	if (document.body.scrollTop != 0) {
		point = { x : document.body.scrollLeft, y : document.body.scrollTop };
	} else {
		point = { x : document.documentElement.scrollLeft, y : document.documentElement.scrollTop };
	}
	return point;
}

function getFileSize(uploader)
{
	try   
	{
		var fso, file; 
		fso = new ActiveXObject("Scripting.FileSystemObject"); 
		file =fso.GetFile(uploader.value); 
		return file.size;
	}   
	catch(e)   
	{
		return  -1;   
	}   
}
function size(s)
{
	if (s >= 1024.0 * 1024.0 * 1024.0)
	{
		return FormatAfterDotNumber('' + (s / (1024.0 * 1024.0 * 1024.0)), 2) + 'GB';
	}
	if (s >= 1024.0 * 1024.0)
	{
		return FormatAfterDotNumber('' + (s / (1024.0 * 1024.0)), 2) + 'MB';
	}
	if (s >= 1024.0)
	{
		return FormatAfterDotNumber('' + (s / (1024.0)), 2) + 'KB';
	}
	return s + 'B';
}
function FormatAfterDotNumber(ValueString, nAfterDotNum )
{
    var ValueString,nAfterDotNum ;
　　var resultStr,nTen;
　　ValueString = ""+ValueString+"";
　　strLen = ValueString.length;
　　dotPos = ValueString.indexOf(".",0);
　　if (dotPos == -1)
        {
　　　　resultStr = ValueString+".";
　　　　for (i=0;i<nAfterDotNum ;i++)
                {
　　　　　　resultStr = resultStr+"0";
　　        }
　　　　return resultStr;
　　}
　　else
        {
　　　　if ((strLen - dotPos - 1) >= nAfterDotNum ){
　　　　　　nAfter = dotPos + nAfterDotNum  + 1;
　　　　　　nTen =1;
　　　　　　for(j=0;j<nAfterDotNum ;j++){
　　　　　　　　nTen = nTen*10;
　　　　　　}
　　　　　　resultStr = Math.round(parseFloat(ValueString)*nTen)/nTen;
　　　　　　return resultStr;
　　　　}
　　　　else{
　　　　　　resultStr = ValueString;
　　　　　　for (i=0;i<(nAfterDotNum  - strLen + dotPos + 1);i++){
　　　　　　　　resultStr = resultStr+"0";
　　　　　　}
　　　　　　return resultStr;
　　　　}
　　}
} 

// --------------------------------------

var tooltip;
document.write("<div id='js_tooltip' style='position:absolute; z-index:1000; visibility:hidden'></div>");
function popLayer(e) {
	var el = e.srcElement ? e.srcElement : e.target;
	if (!el) {
		return;
	}
	if (el.alt != null && el.alt != '') {
		el.tooltip = el.alt;
		el.alt = '';
	}
	if (el.title != null && el.title != '') {
		el.tooltip = el.title;
		el.title = '';
	}
	if (el.tooltip != tooltip) {
		tooltip = el.tooltip;
		var container = $('js_tooltip');
		if (tooltip == null || tooltip == '') {
			container.style.display = 'none';
			container.style.width = "0px";
			container.style.visibility = 'hidden';
		} else {
			container.style.display = '';
			container.style.width = "auto";
			container.innerHTML = tooltip.replace(/\n/g, '<br>');
			if (container.clientWidth > 280) {
				container.style.width = "280px";
			}
			var mouse = { x : e.clientX, y : e.clientY };
			var scrollOffset = getScrollOffset();
			var size = { w : container.clientWidth, h : container.clientHeight };
			var adjustLeft  = mouse.x + 12 + size.w > document.body.clientWidth ?  - size.w - 12 : 12;
			var adjustRight = mouse.y + 12 + size.h > document.body.clientHeight ? - size.h - 12 : 12;
			container.style.left = (mouse.x + scrollOffset.x + adjustLeft) + 'px';
			container.style.top  = (mouse.y + scrollOffset.y + adjustRight) + 'px';
			container.style.visibility = 'visible';
		}
	}
}
document.onmouseover = function(e) {
	if (typeof popLayer == "function") {
		popLayer(ie ? event : e);
	}
}
// --------------------------------------